<template>
    <div>
        <a-tree
            :checkable="checkable" 
            :treeData="treeData"
            :replaceFields="localReplaceFields"
            :checkedKeys="checkedKeys"
            :expandedKeys="expandedKeys"
            :checkStrictly="checkStrictly"
            :autoExpandParent="autoExpandParent"
            :showLine="showLine"
            @check="onCheck"
            @select="onSelect"
            @expand="onExpand">
        </a-tree>
    </div>
</template>

<script>

import {StringToJson,isJSON} from '@/utils/util'
import request from '@/utils/request'
import pageSetting from '../pageSetting'

export default {
    name:'ExtTree',
    props:{
        checkable:{
            type:[Boolean],
            default:false
        },
        
        axiosUrl:{
            type:String,
            required:false
        },
        axiosMethod:{
            type:String,
            default:'post'
        },
        param:{
            type:[String],
            default:()=>{}
        },
        replaceFields:{
            type:[String],
            default:()=>{}
        },
        checkStrictly:{
            type:[Boolean],
            default:false
        },
        autoExpandParent:{
            type:[Boolean],
            default:false
        },
        showLine:{
            type:Boolean,
            default:false
        },
        componentRef:[String,Number],
        treeCheck:{
            type:String,
            default:'onCheck'
        },
        treeSelect:{
            type:String,
            default:'onSelect'
        },
        pageObject:[Object]

    },
    data(){
        return{
            checkedKeys:[],//选中的节点
            halfCheckedKeys:[],//半选中的节点
            expandedKeys:[],//展开的节点
            allKeys:[],//树中所有的节点信息
            treeData:[],
            localReplaceFields:{},
            lacalParam:{},
        }
    },
    created(){

        if(isJSON(this.replaceFields)){
            this.localReplaceFields = StringToJson(this.replaceFields)
        }
        this.initTreeData()
        this.pageObject["tree"+this.componentRef] = this
    },
    watch:{
        treeData(){
           this.getAllKeys()
        },
        // axiosUrl(){
        //     this.initTreeData()
        // },
        // axiosMethod(){
        //     this.initTreeData()
        // },
        // param(){
        //     this.initTreeData()
        // }
    },
    methods:{
        initTreeData(){

            if(!this.axiosUrl){
                return false
            }
            if(isJSON(this.param)){
                this.lacalParam = StringToJson(this.param)
            }

            if(this.axiosMethod==pageSetting.常量.POST请求){
                request({url: this.axiosUrl,method: 'post',data:this.lacalParam}).then((res)=>{
                    if(res.success){
                        this.treeData = res.result
                        setTimeout(() => {this.$bus.$emit('treeCallback', this)}, 200)
                    }
                })
            }else if(this.axiosMethod==pageSetting.常量.GET请求){
                request.get(this.axiosUrl,{params:this.lacalParam}).then((res)=>{
                    if (res.success) {
                        this.treeData = res.result
                        setTimeout(() => {this.$bus.$emit('treeCallback', this)}, 200)
                    }
                })
            }
            
        },
        setParam(param){
            this.lacalParam=param
            this.initTreeData()
        },
        setCheckedKeys(checkedKeys){
            this.checkedKeys=checkedKeys
        },
        onCheck(checkedKeys,e){
            if(this.checkStrictly){
                this.checkedKeys = checkedKeys.checked
            }else{
                this.checkedKeys = checkedKeys
                this.halfCheckedKeys=e.halfCheckedKeys
            }
            let checked={checkedKeys:this.checkedKeys,halfCheckedKeys:this.halfCheckedKeys,checkedNodes:e.checkedNodes}
            this.$emit(this.treeCheck,checked)
        },
        onExpand(expandedKeys,e){
            this.expandedKeys = expandedKeys
        },
        onSelect(selectedKeys,e){
             let vNode ={key:e.node.eventKey,title:e.node.title,value:e.node.value || ''}
             this.$emit(this.treeSelect,vNode)
        },
        //取消所有
        cancelCheckALL(){
            this.checkedKeys=[]
        },
         //选中所有
        checkALL () {
            this.checkedKeys = this.allKeys
        },
        //展开所有
        expandAll(){
            this.expandedKeys = this.allKeys
        },
        //关闭所有
        closeAll(){
            this.expandedKeys = []
        },
        getAllKeys(){
            let that = this
            that.allKeys=[]
            
            if(that.treeData){
                if(that.replaceFields && isJSON(that.replaceFields)){
                    that.localReplaceFields = StringToJson(that.replaceFields)
                }
                
                if(!that.localReplaceFields){
                    that.localReplaceFields={}
                }
                let isReplace = JSON.stringify(that.localReplaceFields) == "{}"
                that.treeData.forEach(function(val, index){
                    that.allKeys.push(isReplace?val["key"]:val[that.localReplaceFields["key"]])
                })

                //如果默认展开，则展开所有节点
                if(that.autoExpandParent){
                    that.expandAll()
                }
            }
        }

    }
}
</script>